/**
 * Jul 13, 2012
 */
package ee.edi.common.service;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.PostProcessInterceptor;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;

import ee.edi.common.db.DataBase;
import ee.edi.common.ServiceException;

/**
 * @author andrei
 *
 */
@Provider
@ServerInterceptor
public class ServicePrePostProcessor implements PreProcessInterceptor, PostProcessInterceptor {

	protected @Context HttpServletRequest servletRequest;

	/* (non-Javadoc)
	 * @see org.jboss.resteasy.spi.interception.PreProcessInterceptor#preProcess(org.jboss.resteasy.spi.HttpRequest, org.jboss.resteasy.core.ResourceMethod)
	 */
	public ServerResponse preProcess(HttpRequest request, ResourceMethod method)
			throws Failure, WebApplicationException {
		try {
			DataBase.open();
		} catch (SQLException e) {
			throw new ServiceException(e);
		}
		// Autendime klientrakenduse:
//		AuthProvider.authenticate(method.getResourceClass(), method.getMethod(), request, servletRequest);
		return null;
	}

	public void postProcess(ServerResponse response) {
		DataBase.commit();
		DataBase.close();
	}

}
